/**
 * Copyright  2025, Hai Yue Xing He  ZHAO LIMIN
 *
 * @author        ZHAO LIMIN
 * @version       1.0.1
 * @since         2025-04
 * HDataType.ts   用于特殊数据类型定义
 */
import { Cartesian3 } from './Cartesian3'
import { Cartesian2 } from './Cartesian2'
import { ICartesian } from './Cartesian'
import { Quaternion } from './Quaternion'
import { defined } from './Defined'
import { DeveloperError } from './DeveloperError'
import { Check } from './Check'
export type UNION_NUMBER_ARRAY =
  | Array<number>
  | number[]
  | Int8Array
  | Uint8Array
  | Int16Array
  | Uint16Array
  | Int32Array
  | Uint32Array
  | Float32Array
  | Float64Array

export function PushNumbersToArrays(
  numbers: UNION_NUMBER_ARRAY,
  ...args:number[]): UNION_NUMBER_ARRAY {
 
  if (!defined(numbers)){
    throw new DeveloperError("入口参数numbers 必须指定");
  }
  Check.typeOf.object("numbers", numbers)
  if (numbers instanceof Array) {
    numbers.push(...args);
    return numbers; 
  } else {
    const length = numbers.length + args.length;
    let new_numbers;    
    if ( numbers instanceof Int8Array) {
      new_numbers = new Int8Array(length);
    }else if ( numbers instanceof Uint8Array) {
      new_numbers = new Uint8Array(length);
    }else if ( numbers instanceof Int16Array) {
      new_numbers = new Int16Array(length); 
    } else if ( numbers instanceof Uint16Array) {
      new_numbers = new Int16Array(length); 
    } else if ( numbers instanceof Int32Array) {
      new_numbers = new Int32Array(length); 
    } else if ( numbers instanceof Uint32Array) {
      new_numbers = new Int32Array(length);
    } else if ( numbers instanceof Float32Array) {
      new_numbers = new Float32Array(length);
    } else if ( numbers instanceof Float64Array) {
      new_numbers = new Float64Array(length);
    } 
    new_numbers!.set(numbers, 0);
    new_numbers!.set(args, numbers.length - args.length);
    return new_numbers!;
  } 
}

export function PopNumberArray(
  numbers: UNION_NUMBER_ARRAY):[number, UNION_NUMBER_ARRAY] {
 
  if (!defined(numbers)){
    throw new DeveloperError("入口参数numbers 必须指定");
  }
  Check.typeOf.object("numbers", numbers)
  if (numbers instanceof Array) {
    const last = numbers.pop()!;    
    return [last, numbers];
  } else {
        const last = numbers[numbers.length - 1];
    const new_numbers = numbers.slice(0, numbers.length - 1);
    return [last, new_numbers];
  } 
}

export type UNION_COORDINATE_ARRAY =
  | Cartesian2[]
  | Cartesian3[]
  | Quaternion[]
  | Array<ICartesian | Quaternion>
  | number[][]
  | Array<number[]>
  | Int8Array
  | Uint8Array // 像素坐标
  | Int16Array
  | Uint16Array // 像素坐标
  | Int32Array
  | Uint32Array // 像素坐标
  | Float32Array
  | Float64Array
  | Array<string> // 坐标用string 记录，如：'123.123,45.678'

export type UNION_SPLINE_POINT_TYPE = string | typeof Cartesian3 | typeof Quaternion
export type UNION_SPLINE_POINTS = ICartesian | Quaternion | number[]
export type UNION_UNSIGNED_INT_ARRAY = number[] | Uint8Array | Uint16Array | Uint32Array

export type UNION_ARRAY_TYPEOF_INT = 
  | typeof Int8Array
  | typeof Uint8Array
  | typeof Int16Array
  | typeof Uint16Array
  | typeof Int32Array
  | typeof Uint32Array
  | typeof Float32Array
  | typeof Float64Array
  | typeof Uint8ClampedArray 

  export type UNION_ARRAY_NUMS = 
  | Array<number>
  | number[]
  | Int8Array
  | Uint8Array
  | Int16Array
  | Uint16Array
  | Int32Array
  | Uint32Array
  | Float32Array
  | Float64Array
  | Uint8ClampedArray 
/**
 *   | Int8Array
  | Uint8Array
  | Int16Array
  | Uint16Array
  | Int32Array
  | Uint32Array
  | Float32Array
  | Float64Array
 */
export function uionarraytonumbers(array: UNION_NUMBER_ARRAY): number[] {
  if (!defined(array)){
    throw new DeveloperError("入口参数array 必须指定");
  }
  Check.typeOf.object("array", array)
  return Array.from(array); 
}
 

  
